package com.xiguthings.waka.service.sysfile.impl;

import java.io.File;
import java.util.List;

import com.xiguthings.waka.dao.mapper.SysFileMapper;
import com.xiguthings.waka.publicUtils.PropertiesUtil;
import com.xiguthings.waka.publicUtils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xiguthings.waka.po.SysFile;
import com.xiguthings.waka.po.SysFileExample;
import com.xiguthings.waka.po.extpo.Page;
import com.xiguthings.waka.service.sysfile.ISysFileService;

import javax.annotation.Resource;

@Service
@Transactional
public class SysFileServiceImpl implements ISysFileService {

    @Resource
    private SysFileMapper sysFileMapper;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public int countByExample(SysFileExample example) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int deleteByExample(SysFileExample example) throws Exception {
        // TODO

        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int deleteByPrimaryKey(String id) throws Exception {
        SysFile sysFile = sysFileMapper.selectByPrimaryKey(id);
        if (sysFile == null) {
            return 0;
        }

        String baseFilePath = PropertiesUtil.getInstance().getPro("config/app.properties", "file.path");
        File file = new File(baseFilePath, sysFile.getFilePath());
        if (file.exists()) {
            //如果文件存在
            if (file.isFile() && file.delete()) {// 如果是文件 且 如果删除成功
                return sysFileMapper.deleteByPrimaryKey(id);// 删除数据库
            }
        } else {
            // 如果文件不存在
            return sysFileMapper.deleteByPrimaryKey(id);// 删除数据库
        }
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int insert(SysFile t) throws Exception {
        t.setFileId(StringUtils.getRandomUUID());
        return sysFileMapper.insert(t);
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int insertSelective(SysFile t) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<SysFile> selectByExample(SysFileExample example) throws Exception {

        return sysFileMapper.selectByExample(example);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public SysFile selectByPrimaryKey(String id) throws Exception {

        return sysFileMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByExampleSelective(SysFile t, SysFileExample example) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByExample(SysFile t, SysFileExample example) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(SysFile t) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByPrimaryKey(SysFile t) throws Exception {

        return sysFileMapper.updateByPrimaryKeySelective(t);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page selectByPage(Page pageEntity, SysFileExample example) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }


}
